#include "mainscene.h"
#include"config.h"

#include <QIcon>
#include<qpainter.h>
#include<QTimer>
QTimer m_Timer;
MainScene::MainScene(QWidget *parent): QWidget(parent)
{

    initScene();

}
void MainScene::initScene()
{
    //初始化窗口大小
    setFixedSize(GAME_WIDTH,GAME_HEIGHT);
setWindowIcon(QIcon( GAME_ICON));
    //设置窗口标题
    setWindowTitle(GAME_TITLE);
 m_Timer.setInterval(GAME_RATE);
}
void MainScene::playGame()
{
    //启动定时器
    m_Timer.start();

    //监听定时器
    connect(&m_Timer,&QTimer::timeout,[=](){
        //更新游戏中元素的坐标
        updatePosition();
        //重新绘制图片
        update();
    });
}

void MainScene::updatePosition()
{
    //更新地图坐标
    m_map.mapPosition();
}

void MainScene::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);

    //绘制地图
    painter.drawPixmap(0,m_map.m_map1_posY , m_map.m_map1);
    painter.drawPixmap(0,m_map.m_map2_posY , m_map.m_map2);

    //绘制英雄
    painter.drawPixmap(m_hero.m_X,m_hero.m_Y,m_hero.m_Plane);
}
void MainScene::keyPressEvent(QKeyEvent *event) // 飞机随之移动
{
    int key=event->key();
       switch (key) {
       case Qt::Key_Up:GAME_Heigth-hero_plane.plane_rect.height()-10;   //1代表上
           break;
       case Qt::Key_Down:GAME_Heigth-hero_plane.plane_rect.height()+10;   //2代表下
           break;
       case Qt::Key_Left:GAME_Width-hero_plane.plane_rect.width()+10;   //3代表左
           break;
       case Qt::Key_Right:GAME_Width-hero_plane.plane_rect.width()-10;   //4代表右
           break;
       default:;
       }

    if(x<=0)
    {
        x=0;
    }
    if(x>GAME_Width-hero_plane.plane_rect.width())
    {
        x=GAME_Width-hero_plane.plane_rect.width();
    }
    if(y<=0)
    {
        y=0;
    }
    if(y>=GAME_Heigth-hero_plane.plane_rect.height())
    {
        y=GAME_Heigth-hero_plane.plane_rect.height();
    }
    hero_plane.setPlanePosition(x,y);
}

MainScene::~MainScene()
{

}
